import java.io.IOException;
import java.util.HashMap;
import java.util.TreeMap;

import org.apache.http.client.ClientProtocolException;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.HttpUtil;
import com.bootdo.common.wx.WeiXinUtil;
import com.google.gson.JsonObject;

public class cnmhhhh {
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
		  String accessToken =WeiXinUtil.getOauth2AccessToken2("wxcb74c2a7fb1fd161", "3ba771e951e1e0e917b7dcd777a25e06");
		String createForeverTicket = createForeverTicket(accessToken,"yq@257@wxcb74c2a7fb1fd161@3ba771e951e1e0e917b7dcd777a25e06@3");
		String aa=" https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+createForeverTicket;
		System.out.println(aa);
		
//		String aa="qrscene_yq@257@wxcb74c2a7fb1fd161@3ba771e951e1e0e917b7dcd777a25e06";
//		String[] split = aa.split("qrscene_");
//		System.out.println(split[1]);
		
		
	}
	
    /**
     * 创建永久二维码(数字)
     *
     * @param accessToken
     * @param sceneId     场景Id
     * @return
     */
    public static String createForeverTicket(String accessToken, int sceneId) {
    	String resurl="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken;
        TreeMap<String, String> params = new TreeMap<>();
        params.put("access_token", accessToken);
        // output data
        JsonObject data = new JsonObject();
        data.addProperty("action_name", "QR_LIMIT_SCENE");
        JsonObject scene = new JsonObject();
        scene.addProperty("scene_id", sceneId);
        JsonObject actionInfo = new JsonObject();
        actionInfo.add("scene", scene);
        data.add("action_info", actionInfo);
        String result = HttpUtil.doPost(resurl, params, data.toString());
        return result;
    }
    
    /**
     * 创建永久二维码(字符串)
     *
     * @param accessToken
     * @param sceneStr    场景str
     * @return
     */
    public static String createForeverTicket(String accessToken, String sceneStr) {
    	String resurl="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+accessToken;
        TreeMap<String, String> params = new TreeMap<>();
        params.put("access_token", accessToken);
        // output data
        JsonObject data = new JsonObject();
        data.addProperty("action_name", "QR_LIMIT_STR_SCENE");
        JsonObject actionInfo = new JsonObject();
        JsonObject scene = new JsonObject();
        scene.addProperty("scene_str", sceneStr);
        actionInfo.add("scene", scene);
        data.add("action_info", actionInfo);
        String result = HttpUtil.doPost(resurl, params, data.toString());
        HashMap hashMap = JSON.parseObject(result, HashMap.class);
        return (String) hashMap.get("ticket");
    }

}
